package com.jackchen.common.filter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.jackchen.service.UserService;

public class I18nInterceptor implements HandlerInterceptor{
	
	private static final Logger log = Logger.getLogger(I18nInterceptor.class);
	
	@Autowired
	private SessionLocaleResolver localeResolver;
	
	@Autowired
	private UserService userService;
	
	public boolean preHandle(HttpServletRequest a_request,HttpServletResponse a_response,Object a_handler) throws Exception{
		/*
		HttpSession httpSession = a_request.getSession();
		String userName =  (String)httpSession.getAttribute(LoginFilter.SESSION_USER_NAME);
		if(null == userName){
			log.debug("user name is null ,you haven't loged in!");
			return false;
		}
		User loginUser = userService.queryUserByUserName(userName);
		if(1 == (int)loginUser.getLanguage()){
			localeResolver.setLocale(a_request, a_response, Locale.SIMPLIFIED_CHINESE);
		}else{
			localeResolver.setLocale(a_request, a_response, Locale.US);
			log.debug("session set attribute:"+a_request.getSession().getAttribute(SessionLocaleResolver.class.getName() + ".LOCALE"));
		}
		*/
		return true;
		
	}
	
	public void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,
						   ModelAndView modelAndView) throws Exception{
		LocaleResolver  localeResolver=RequestContextUtils.getLocaleResolver(request);
		if(null == localeResolver){
			log.debug("LocaleResolver class name is null");
		}else{
			log.debug("LocaleResolver class name is"+localeResolver.getClass());
		}
		
	}

	public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,
								Exception e) throws Exception{
		//log.debug("");
	}
}
